﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using TimKen.DataEntity;
using System.Globalization;
namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类OrderItem。
    /// </summary>
    public class OrderItemDAO
    {
        public OrderItemDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string OrderItemID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OrderItemID", SqlDbType.NVarChar,50)};
            parameters[0].Value = OrderItemID;

            int result = DbHelperSQL.RunProcedure("UP_OrderItem_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.OrderItem model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OrderItemID", SqlDbType.NVarChar,50),
					new SqlParameter("@OrderID", SqlDbType.NVarChar,50),
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.VarChar,50),
					new SqlParameter("@BarCode", SqlDbType.VarChar,50),
					new SqlParameter("@BatchNumbers", SqlDbType.VarChar,50),
					new SqlParameter("@BasicPrice", SqlDbType.Float,8),
					new SqlParameter("@Discount", SqlDbType.Float,8),
					new SqlParameter("@FinalPrice", SqlDbType.Float,8),
					new SqlParameter("@TaxRate", SqlDbType.Float,8),
					new SqlParameter("@Quantity", SqlDbType.Float,8),
                    new SqlParameter("@Unit", SqlDbType.NVarChar,50),
					new SqlParameter("@TotalAmount", SqlDbType.Float,8),
					new SqlParameter("@BaseAmount", SqlDbType.Float,8),
					new SqlParameter("@TaxAmount", SqlDbType.Float,8),
					new SqlParameter("@DeliveryDate", SqlDbType.DateTime),
					new SqlParameter("@DeliveryQty", SqlDbType.Float,8),
					new SqlParameter("@Weight", SqlDbType.Decimal),
					new SqlParameter("@TotalWeight", SqlDbType.Float,8),
					new SqlParameter("@WeightUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@ShipType", SqlDbType.Int,4),
					new SqlParameter("@ShipToCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ItemRemark", SqlDbType.NVarChar,255),
					new SqlParameter("@ItemStatus", SqlDbType.Int,4),
                    new SqlParameter("@ProductID", SqlDbType.Int)           
                                        };
            parameters[0].Value = model.OrderItemID;
            parameters[1].Value = model.OrderID;
            parameters[2].Value = model.ItemNo;
            parameters[3].Value = model.ItemCode;
            parameters[4].Value = model.BarCode;
            parameters[5].Value = model.BatchNumbers;
            parameters[6].Value = model.BasicPrice;
            parameters[7].Value = model.Discount;
            parameters[8].Value = model.FinalPrice;
            parameters[9].Value = model.TaxRate;
            parameters[10].Value = model.Quantity;
            parameters[11].Value = model.Unit;
            parameters[12].Value = model.TotalAmount;
            parameters[13].Value = model.BaseAmount;
            parameters[14].Value = model.TaxAmount;
            parameters[15].Value = model.DeliveryDate;
            parameters[16].Value = model.DeliveryQty;
            parameters[17].Value = model.Weight;
            parameters[18].Value = model.TotalWeight;
            parameters[19].Value = model.WeightUnit;
            parameters[20].Value = model.ShipType;
            parameters[21].Value = model.ShipToCode;
            parameters[22].Value = model.ItemRemark;
            parameters[23].Value = model.ItemStatus;
            parameters[24].Value = model.ProductID;
          

            DbHelperSQL.RunProcedure("UP_OrderItem_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.OrderItem model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OrderItemID", SqlDbType.NVarChar,50),
					new SqlParameter("@OrderID", SqlDbType.NVarChar,50),
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.VarChar,50),
					new SqlParameter("@BarCode", SqlDbType.VarChar,50),
					new SqlParameter("@BatchNumbers", SqlDbType.VarChar,50),
					new SqlParameter("@BasicPrice", SqlDbType.Float,8),
					new SqlParameter("@Discount", SqlDbType.Float,8),
					new SqlParameter("@FinalPrice", SqlDbType.Float,8),
					new SqlParameter("@TaxRate", SqlDbType.Float,8),
					new SqlParameter("@Quantity", SqlDbType.Float,8),
                    new SqlParameter("@Unit", SqlDbType.NVarChar,50),
					new SqlParameter("@TotalAmount", SqlDbType.Float,8),
					new SqlParameter("@BaseAmount", SqlDbType.Float,8),
					new SqlParameter("@TaxAmount", SqlDbType.Float,8),
					new SqlParameter("@DeliveryDate", SqlDbType.DateTime),
					new SqlParameter("@DeliveryQty", SqlDbType.Float,8),
					new SqlParameter("@Weight", SqlDbType.Decimal),
					new SqlParameter("@TotalWeight", SqlDbType.Float,8),
					new SqlParameter("@WeightUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@ShipType", SqlDbType.Int,4),
					new SqlParameter("@ShipToCode", SqlDbType.NVarChar,50),
					new SqlParameter("@ItemRemark", SqlDbType.NVarChar,255),
					new SqlParameter("@ItemStatus", SqlDbType.Int,4),
                    new SqlParameter("@ProductID", SqlDbType.Int),
                    new SqlParameter("@RequestDate",SqlDbType.DateTime)                    
                                        };
            parameters[0].Value = model.OrderItemID;
            parameters[1].Value = model.OrderID;
            parameters[2].Value = model.ItemNo;
            parameters[3].Value = model.ItemCode;
            parameters[4].Value = model.BarCode;
            parameters[5].Value = model.BatchNumbers;
            parameters[6].Value = model.BasicPrice;
            parameters[7].Value = model.Discount;
            parameters[8].Value = model.FinalPrice;
            parameters[9].Value = model.TaxRate;
            parameters[10].Value = model.Quantity;
            parameters[11].Value = model.Unit;
            parameters[12].Value = model.TotalAmount;
            parameters[13].Value = model.BaseAmount;
            parameters[14].Value = model.TaxAmount;
            parameters[15].Value = model.DeliveryDate;
            parameters[16].Value = model.DeliveryQty;
            parameters[17].Value = model.Weight;
            parameters[18].Value = model.TotalWeight;
            parameters[19].Value = model.WeightUnit;
            parameters[20].Value = model.ShipType;
            parameters[21].Value = model.ShipToCode;
            parameters[22].Value = model.ItemRemark;
            parameters[23].Value = model.ItemStatus;
            parameters[24].Value = model.ProductID;
            parameters[25].Value = model.RequestDate;

            DbHelperSQL.RunProcedure("UP_OrderItem_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string OrderItemID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@OrderItemID", SqlDbType.NVarChar,50)};
            parameters[0].Value = OrderItemID;

            DbHelperSQL.RunProcedure("UP_OrderItem_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.OrderItem GetModel(string OrderItemID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@OrderItemID", SqlDbType.NVarChar,50)};
            parameters[0].Value = OrderItemID;

            TimKen.DataEntity.OrderItem model = new TimKen.DataEntity.OrderItem();
            DataSet ds = DbHelperSQL.RunProcedure("UP_OrderItem_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.OrderItemID = ds.Tables[0].Rows[0]["OrderItemID"].ToString();
                model.OrderID = ds.Tables[0].Rows[0]["OrderID"].ToString();
                if (ds.Tables[0].Rows[0]["ItemNo"].ToString() != "")
                {
                    model.ItemNo = int.Parse(ds.Tables[0].Rows[0]["ItemNo"].ToString());
                }
                model.ItemCode = ds.Tables[0].Rows[0]["ItemCode"].ToString();
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                model.BarCode = ds.Tables[0].Rows[0]["BarCode"].ToString();
                model.BatchNumbers = ds.Tables[0].Rows[0]["BatchNumbers"].ToString();
                if (ds.Tables[0].Rows[0]["BasicPrice"].ToString() != "")
                {
                    model.BasicPrice = decimal.Parse(ds.Tables[0].Rows[0]["BasicPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Discount"].ToString() != "")
                {
                    model.Discount = decimal.Parse(ds.Tables[0].Rows[0]["Discount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FinalPrice"].ToString() != "")
                {
                    model.FinalPrice = decimal.Parse(ds.Tables[0].Rows[0]["FinalPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxRate"].ToString() != "")
                {
                    model.TaxRate = decimal.Parse(ds.Tables[0].Rows[0]["TaxRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Quantity"].ToString() != "")
                {
                    model.Quantity = decimal.Parse(ds.Tables[0].Rows[0]["Quantity"].ToString());
                }

                model.Unit = ds.Tables[0].Rows[0]["Unit"].ToString();

                if (ds.Tables[0].Rows[0]["TotalAmount"].ToString() != "")
                {
                    model.TotalAmount = decimal.Parse(ds.Tables[0].Rows[0]["TotalAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseAmount"].ToString() != "")
                {
                    model.BaseAmount = decimal.Parse(ds.Tables[0].Rows[0]["BaseAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaxAmount"].ToString() != "")
                {
                    model.TaxAmount = decimal.Parse(ds.Tables[0].Rows[0]["TaxAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeliveryDate"].ToString() != "")
                {
                    model.DeliveryDate = DateTime.Parse(ds.Tables[0].Rows[0]["DeliveryDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeliveryQty"].ToString() != "")
                {
                    model.DeliveryQty = decimal.Parse(ds.Tables[0].Rows[0]["DeliveryQty"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Weight"].ToString() != "")
                {
                    //model.Weight = decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString());
                    model.Weight = Decimal.Parse(ds.Tables[0].Rows[0]["Weight"].ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent);
                }
                if (ds.Tables[0].Rows[0]["TotalWeight"].ToString() != "")
                {
                    model.TotalWeight = decimal.Parse(ds.Tables[0].Rows[0]["TotalWeight"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WeightUnit"].ToString() != "")
                {
                    model.WeightUnit = ds.Tables[0].Rows[0]["WeightUnit"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ShipType"].ToString() != "")
                {
                    model.ShipType = int.Parse(ds.Tables[0].Rows[0]["ShipType"].ToString());
                }
                model.ShipToCode = ds.Tables[0].Rows[0]["ShipToCode"].ToString();
                model.ItemRemark = ds.Tables[0].Rows[0]["ItemRemark"].ToString();
                if (ds.Tables[0].Rows[0]["ItemStatus"].ToString() != "")
                {
                    model.ItemStatus = int.Parse(ds.Tables[0].Rows[0]["ItemStatus"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OrderItemID,OrderID,ItemNo,ItemCode,ProductID,BarCode,BatchNumbers,BasicPrice,Discount,FinalPrice,TaxRate,Quantity,Unit,TotalAmount,BaseAmount,TaxAmount,DeliveryDate,DeliveryQty,Weight,TotalWeight,WeightUnit,ShipType,ShipToCode,ItemRemark,ItemStatus ");
            strSql.Append(" FROM OrderItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  成员方法

        public DataSet GetOrderItems(string OrderID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@OrderID", SqlDbType.VarChar,50)};
            parameters[0].Value = OrderID;

            return DbHelperSQL.RunProcedure("UP_OrderItem_GetOrderItems", parameters, "OrderItem");
        }


        public List<OrderItem> GetOrderItemList(string OrderID)
        {
           DataSet  ds  =   GetOrderItems(OrderID);
           
            List<OrderItem>    result = new List<OrderItem>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                OrderItem model = new OrderItem();
                model.OrderItemID = ds.Tables[0].Rows[i]["OrderItemID"].ToString();
                model.OrderID = ds.Tables[0].Rows[i]["OrderID"].ToString();
                if (ds.Tables[0].Rows[i]["ItemNo"].ToString() != "")
                {
                    model.ItemNo = int.Parse(ds.Tables[0].Rows[i]["ItemNo"].ToString());
                }
                model.ItemCode = ds.Tables[0].Rows[i]["ItemCode"].ToString();
                if (ds.Tables[0].Rows[i]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[i]["ProductID"].ToString());
                }
                model.BarCode = ds.Tables[0].Rows[i]["BarCode"].ToString();
                model.BatchNumbers = ds.Tables[0].Rows[i]["BatchNumbers"].ToString();
                if (ds.Tables[0].Rows[i]["BasicPrice"].ToString() != "")
                {
                    model.BasicPrice = decimal.Parse(ds.Tables[0].Rows[i]["BasicPrice"].ToString());
                }
                if (ds.Tables[0].Rows[i]["Discount"].ToString() != "")
                {
                    model.Discount = decimal.Parse(ds.Tables[0].Rows[i]["Discount"].ToString());
                }
                if (ds.Tables[0].Rows[i]["FinalPrice"].ToString() != "")
                {
                    model.FinalPrice = decimal.Parse(ds.Tables[0].Rows[i]["FinalPrice"].ToString());
                }
                if (ds.Tables[0].Rows[i]["TaxRate"].ToString() != "")
                {
                    model.TaxRate = decimal.Parse(ds.Tables[0].Rows[i]["TaxRate"].ToString());
                }
                if (ds.Tables[0].Rows[i]["Quantity"].ToString() != "")
                {
                    model.Quantity = decimal.Parse(ds.Tables[0].Rows[i]["Quantity"].ToString());
                }

                model.Unit = ds.Tables[0].Rows[i]["Unit"].ToString();

                if (ds.Tables[0].Rows[i]["TotalAmount"].ToString() != "")
                {
                    model.TotalAmount = decimal.Parse(ds.Tables[0].Rows[i]["TotalAmount"].ToString());
                }
                if (ds.Tables[0].Rows[i]["BaseAmount"].ToString() != "")
                {
                    model.BaseAmount = decimal.Parse(ds.Tables[0].Rows[i]["BaseAmount"].ToString());
                }
                if (ds.Tables[0].Rows[i]["TaxAmount"].ToString() != "")
                {
                    model.TaxAmount = decimal.Parse(ds.Tables[0].Rows[i]["TaxAmount"].ToString());
                }
                if (ds.Tables[0].Rows[i]["DeliveryDate"].ToString() != "")
                {
                    model.DeliveryDate = DateTime.Parse(ds.Tables[0].Rows[i]["DeliveryDate"].ToString());
                }
                if (ds.Tables[0].Rows[i]["DeliveryQty"].ToString() != "")
                {
                    model.DeliveryQty = decimal.Parse(ds.Tables[0].Rows[i]["DeliveryQty"].ToString());
                }
                if (ds.Tables[0].Rows[i]["Weight"].ToString() != "")
                {
                    model.Weight = decimal.Parse(ds.Tables[0].Rows[i]["Weight"].ToString());
                }
                if (ds.Tables[0].Rows[i]["TotalWeight"].ToString() != "")
                {
                    model.TotalWeight = decimal.Parse(ds.Tables[0].Rows[i]["TotalWeight"].ToString());
                }
                if (ds.Tables[0].Rows[i]["WeightUnit"].ToString() != "")
                {
                    model.WeightUnit = ds.Tables[0].Rows[i]["WeightUnit"].ToString();
                }
                if (ds.Tables[0].Rows[i]["ShipType"].ToString() != "")
                {
                    model.ShipType = int.Parse(ds.Tables[0].Rows[i]["ShipType"].ToString());
                }
                model.ShipToCode = ds.Tables[0].Rows[i]["ShipToCode"].ToString();
                model.ItemRemark = ds.Tables[0].Rows[i]["ItemRemark"].ToString();
                if (ds.Tables[0].Rows[i]["ItemStatus"].ToString() != "")
                {
                    model.ItemStatus = int.Parse(ds.Tables[0].Rows[i]["ItemStatus"].ToString());
                }


                if (ds.Tables[0].Rows[0]["RequestDate"].ToString() != "")
                {
                    model.RequestDate = DateTime.Parse(ds.Tables[0].Rows[0]["RequestDate"].ToString());
                }

                result.Add(model);
            }

            return result;
        }

    }
}
